package com.yr.ryjk.hospital;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.hospital.entity.Hospital;
import com.yr.ryjk.hospital.service.IHospitalService;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.util.JSONUtil;
/**
 * 医院
 * @author Rick
 *
 */
@Controller
@RequestMapping("/hospital")
@ResponseBody
public class HospitalController {
	@Autowired
	private IHospitalService hospitalService;
	
	/**
	 * 后台分页获取列表
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Map<String, Object> list(@RequestParam(value="pageNo",defaultValue="1")int pageNo,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize,
			String province,String city,String county) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,
				hospitalService.getListOfPage(pageNo,pageSize,province,city,county));
	}
	
	/**
	 * 按id获取详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public Map<String, Object> get(@RequestParam(value="id") @NotBlank String id) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,hospitalService.getInfoById(id));
	}
	

	/**
	 * 获取所有医院
	 * @return
	 */
	@RequestMapping("/all")
	@ResponseBody
	public Map<String, Object> getAllHospital() {
		List<Hospital> list=hospitalService.getAllHospital();
		return JSONUtil.getJSON(ErrorType.SUCCESS,list);
	}
	
	
	/**
	 * 后台修改医院信息
	 * @param id
	 * @param name
	 * @param level
	 * @param location
	 * @param county 县区id,通过这即可决定省市
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public Map<String, Object> edit(@RequestParam(value="id") @NotBlank String id,
			@RequestParam(value="name") @NotBlank String name,
			@RequestParam(value="level") @NotBlank String level,
			@RequestParam(value="location") @NotBlank String location,
			@RequestParam(value="county") @NotBlank String county) {
		hospitalService.update(id,name,level,location,county);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 后台增加医院信息
	 * @param id
	 * @param name
	 * @param level
	 * @param location
	 * @param county
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public Map<String, Object> add(
			@RequestParam(value="name") @NotBlank String name,
			@RequestParam(value="level") @NotBlank String level,
			@RequestParam(value="location") @NotBlank String location,
			@RequestParam(value="county") @NotBlank int county){
		hospitalService.insert(name,level,location,county);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
		
	} 
	/**
	 * 模糊查询名称类似的医院,不拦截
	 * @param name
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/likely",method=RequestMethod.GET)
	public Map<String, Object> likelyFind(@RequestParam(value="name") @NotBlank String name) throws UnsupportedEncodingException {
		List<Hospital> list=hospitalService.getLikelyList(name);
//			name=URLDecoder.decode(name,"UTF-8");
	    name=new String(name.getBytes("iso8859-1"),"UTF-8");
		return JSONUtil.getJSON(ErrorType.SUCCESS,list);
	}
}